import { createRouter,createWebHashHistory } from "vue-router";
const router = createRouter({
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    { path: "/login", component: () => import("@/views/Login/index.vue") },
    {
      path: "/",
      component: () => import("@/views/Layout/index.vue"),
      children: [
        { path: "/", component: () => import("@/views/User/User.vue") },
        { path: "/schedule", component: () => import("@/views/School/Subject.vue") },
        { path: "/wall", component: () => import("@/views/School/ConfessionWall.vue") },
        { path: "/excuse", component: () => import("@/views/School/Excuse.vue") },
        { path: "/user", component: () => import("@/views/User/User.vue") },
        { path: "/password", component: () => import("@/views/User/Userpassword.vue") },
        { path: "/picture", component: () => import("@/views/User/Userpicture.vue") },
        { path: "/profile", component: () => import("@/views/User/Userprofile.vue") },
        { path: "/pushwall", component: () => import("@/views/School/Pushwall.vue") },
      ],
    },
  ],
  scrollBehavior() {
    return {
      top: 0,
    };
  },
});

//添加路由守卫

router.beforeEach(async (to) => {
  //没有token，则跳转到登陆页面
  let user;
  try {
    user = JSON.parse(localStorage.getItem("user"));
  } catch (err) {
    console.log("用户数据解析失败，请检查本地仓库", err);
  }
  if (!user?.useInfo?.token && to.path !== "/login") {
    console.log("跳转!");
    return "/login";
  }
  //鉴权成功则允许导航
  return true;
});

export default router;
